{% extends 'mobile_app/base.html' %}
{% load static %}

{% block title %}号码管理 - 郑州移不动{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2 class="mb-0">
                <i class="fas fa-phone text-primary me-2"></i>号码管理
            </h2>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addPhoneModal">
                <i class="fas fa-plus me-2"></i>添加号码
            </button>
        </div>
    </div>
</div>

<!-- 号码列表 -->
<div class="row g-4">
    {% for phone in phone_numbers %}
    <div class="col-md-6 col-lg-4">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div>
                        <h5 class="card-title mb-1">
                            <i class="fas fa-phone text-primary me-2"></i>{{ phone.number }}
                        </h5>
                        {% if phone.is_primary %}
                            <span class="badge bg-primary">主号码</span>
                        {% endif %}
                    </div>
                    <div class="dropdown">
                        <button class="btn btn-sm btn-outline-secondary" type="button" data-bs-toggle="dropdown">
                            <i class="fas fa-ellipsis-v"></i>
                        </button>
                        <ul class="dropdown-menu">
                            <li>
                                <a class="dropdown-item" href="{% url 'mobile_app:balance_query' phone.id %}">
                                    <i class="fas fa-search me-2"></i>查询余额
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item" href="{% url 'mobile_app:plan_management' phone.id %}">
                                    <i class="fas fa-cog me-2"></i>套餐管理
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item" href="{% url 'mobile_app:payment' phone.id %}">
                                    <i class="fas fa-credit-card me-2"></i>立即缴费
                                </a>
                            </li>
                            <li><hr class="dropdown-divider"></li>
                            {% if not phone.is_primary %}
                            <li>
                                <form method="post" class="d-inline">
                                    {% csrf_token %}
                                    <input type="hidden" name="phone_id" value="{{ phone.id }}">
                                    <button type="submit" name="set_primary" class="dropdown-item">
                                        <i class="fas fa-star me-2"></i>设为主号码
                                    </button>
                                </form>
                            </li>
                            {% endif %}
                            <li>
                                <form method="post" class="d-inline" onsubmit="return confirm('确定要删除这个号码吗？')">
                                    {% csrf_token %}
                                    <input type="hidden" name="phone_id" value="{{ phone.id }}">
                                    <button type="submit" name="delete_phone" class="dropdown-item text-danger">
                                        <i class="fas fa-trash me-2"></i>删除号码
                                    </button>
                                </form>
                            </li>
                        </ul>
                    </div>
                </div>

                <!-- 余额信息 -->
                <div class="row text-center mb-3">
                    <div class="col-4">
                        <div class="border-end">
                            <h6 class="text-success mb-0">¥{{ phone.balance.amount|floatformat:2 }}</h6>
                            <small class="text-muted">余额</small>
                        </div>
                    </div>
                    <div class="col-4">
                        <div class="border-end">
                            <h6 class="text-info mb-0">{{ phone.balance.data_remaining }}MB</h6>
                            <small class="text-muted">流量</small>
                        </div>
                    </div>
                    <div class="col-4">
                        <h6 class="text-warning mb-0">{{ phone.balance.voice_remaining }}分钟</h6>
                        <small class="text-muted">语音</small>
                    </div>
                </div>

                <!-- 套餐信息 -->
                <div class="mb-3">
                    {% if phone.phone_plan %}
                        <h6 class="text-primary">{{ phone.phone_plan.plan.name }}</h6>
                        <small class="text-muted">¥{{ phone.phone_plan.plan.monthly_fee }}/月</small>
                    {% else %}
                        <span class="text-muted">未选择套餐</span>
                    {% endif %}
                </div>

                <!-- 操作按钮 -->
                <div class="d-grid gap-2">
                    <a href="{% url 'mobile_app:balance_query' phone.id %}" class="btn btn-outline-primary btn-sm">
                        <i class="fas fa-search me-1"></i>详细查询
                    </a>
                </div>
            </div>
        </div>
    </div>
    {% empty %}
    <div class="col-12">
        <div class="card border-0 shadow-sm">
            <div class="card-body text-center py-5">
                <i class="fas fa-phone-slash text-muted mb-4" style="font-size: 4rem;"></i>
                <h4 class="text-muted mb-3">还没有添加手机号码</h4>
                <p class="text-muted mb-4">点击上方按钮添加您的第一个手机号码</p>
                <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addPhoneModal">
                    <i class="fas fa-plus me-2"></i>添加号码
                </button>
            </div>
        </div>
    </div>
    {% endfor %}
</div>

<!-- 添加号码模态框 -->
<div class="modal fade" id="addPhoneModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">
                    <i class="fas fa-plus me-2"></i>添加手机号码
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="post" id="addPhoneForm">
                {% csrf_token %}
                <input type="hidden" name="add_phone" value="add_phone">
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="{{ phone_form.number.id_for_label }}" class="form-label">手机号码</label>
                        <div class="input-group">
                            <span class="input-group-text">
                                <i class="fas fa-phone"></i>
                            </span>
                            {{ phone_form.number }}
                        </div>
                        {% if phone_form.number.errors %}
                            <div class="text-danger small mt-1">
                                {% for error in phone_form.number.errors %}
                                    <div>{{ error }}</div>
                                {% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">请输入11位有效手机号码</div>
                    </div>
                    
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i>
                        <strong>提示：</strong>每个用户最多可以添加5个手机号码。第一个添加的号码将自动设为主号码。
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" name="add_phone" value="add_phone" class="btn btn-primary" id="addPhoneBtn">
                        <i class="fas fa-plus me-2"></i>添加号码
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // 为表单字段添加Bootstrap样式
    $('input[type="text"]').addClass('form-control');
    
    // 手机号码格式验证
    $('input[name="number"]').on('input', function() {
        var value = $(this).val();
        var phoneRegex = /^1[3-9]\d{9}$/;
        
        if (value.length === 11) {
            if (phoneRegex.test(value)) {
                $(this).removeClass('is-invalid').addClass('is-valid');
            } else {
                $(this).removeClass('is-valid').addClass('is-invalid');
            }
        } else {
            $(this).removeClass('is-valid is-invalid');
        }
    });
    
    // 添加号码表单提交处理
    $('#addPhoneForm').on('submit', function(e) {
        var phoneNumber = $('input[name="number"]').val();
        
        // 基本的前端验证
        if (!phoneNumber || phoneNumber.length !== 11) {
            e.preventDefault();
            alert('请输入11位有效手机号码');
            return false;
        }
        
        var phoneRegex = /^1[3-9]\d{9}$/;
        if (!phoneRegex.test(phoneNumber)) {
            e.preventDefault();
            alert('手机号码格式不正确');
            return false;
        }
        
        // 显示加载状态
        $('#addPhoneBtn').html('<i class="fas fa-spinner fa-spin me-2"></i>添加中...');
        
        // 允许表单提交，让服务器端处理其他验证
        return true;
    });
    
    // 重置表单状态
    $('#addPhoneModal').on('hidden.bs.modal', function() {
        $('#addPhoneForm')[0].reset();
        $('input[name="number"]').removeClass('is-valid is-invalid');
        $('#addPhoneBtn').prop('disabled', false).html('<i class="fas fa-plus me-2"></i>添加号码');
    });
});
</script>
{% endblock %}
